package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping("/roles")
@Slf4j
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 新增角色
     */
    @Log
    @PostMapping
    public Result add(@RequestBody Role role) {
        log.info("新增角色 {}", role);
        roleService.add(role);
        return Result.success();
    }

    /**
     * 查询角色列表
     */
    @GetMapping
    public Result listRole(String name, String label,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("查询角色列表 name: {}, status: {}, page: {}, pageSize: {}", name, label, page, pageSize);
        PageResult<Role> roles = roleService.listRole(name, label, page, pageSize);
        return Result.success(roles);
    }

    /**
     * 根据ID查询角色回显
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据ID查询部门回显 id: {}", id);
        Role role = roleService.getById(id);
        return Result.success(role);
    }

    /**
     * 修改角色
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Role role) {
        log.info("修改角色 role: {}", role);
        roleService.update(role);
        return Result.success();
    }


    /**
     * 删除角色
     */
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除角色 id: {}", id);
        roleService.deleteByIds(id);
        return Result.success();
    }

    /**
     * 查询所有角色
     */
    @GetMapping("/list")
    public Result list() {
        log.info("查询所有角色");
        List<Role> roles = roleService.list();
        return Result.success(roles);
    }

}
